package com.lin.controller.user;

import com.lin.common.context.BaseContext;
import com.lin.common.result.Result;
import com.lin.entity.AlbumEntity;
import com.lin.service.AlbumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/album")
@Api(tags = "相册相关接口")
@Slf4j
public class AlbumController {


    @Autowired
    private AlbumService albumService;

    /**
     * 获取相册
     * @return
     */
    @GetMapping
    @ApiOperation("获取相册")
    public Result<AlbumEntity> getAlbum(@RequestBody Integer userId){
        log.info("获取用户相册：{}",userId);
        AlbumEntity albumEntity = albumService.getAlbum(userId);
        return Result.success(albumEntity);
    }

    /**
     * 新建相册
     * @param albumEntity
     * @return
     */
    @PostMapping
    @ApiOperation("新建相册")
    public Result<AlbumEntity> saveClt(@RequestBody AlbumEntity albumEntity){
        log.info("新建相册：{}",albumEntity);
        albumEntity.setCreateTime(LocalDateTime.now());
        albumEntity.setCreatePId(BaseContext.getCurrentId());
        albumService.saveAlbum(albumEntity);
        return Result.success(albumEntity);
    }

    /**
     * 更新收藏夹
     * @param albumEntity
     * @return
     */
    @PutMapping
    @ApiOperation("更新相册")
    public Result<AlbumEntity> updateClt(@RequestBody AlbumEntity albumEntity){
        log.info("更新相册：{}",albumEntity);
        albumService.updateAlbum(albumEntity);
        return Result.success(albumEntity);
    }

    /**
     * 删除相册
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除相册")
    public Result deleteClt(@RequestBody Integer id){
        log.info("删除相册：{}",id);
        albumService.deleteAlbum(id);
        return Result.success();
    }
}
